package com.foreknow.controller;

import com.foreknow.entity.User;
import com.foreknow.service.UserService;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet({ "/api/user", "/api/sign-in", "/api/sign-up" })
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UserService userService;

	public UserServlet() {
		super();
		userService = new UserService();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String uri = request.getRequestURI();
		Map<String, Object> data = new HashMap<>();
		Gson gson = new Gson();
		if (uri.endsWith("/api/sign-in")) {
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			if (userService.signIn(username, password)) {
				data.put("errorCode", 0);
				data.put("message", "登录成功！");
				User user = userService.getUserByName(username);
				user.setPassword("");
				data.put("user", user);
			} else {
				data.put("errorCode", -1);
				data.put("message", "登录失败！");
				data.put("user", null);
			}
		} else if (uri.endsWith("/api/sign-up")) {
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			String account = request.getParameter("account");
			String phone = request.getParameter("phone");
			String email = request.getParameter("email");
			User user = new User(null, username, account, password, phone, email, null);
			if (userService.signUp(user)) {
				user = userService.getUserByName(username);
				data.put("errorCode", 0);
				data.put("message", "注册成功，正在登录！");
				user.setPassword("");
				data.put("user", user);
			} else {
				data.put("errorCode", -1);
				data.put("message", "注册失败！");
			}
		}else if (uri.endsWith("/api/user")) {
			Integer id = Integer.valueOf(request.getParameter("id"));
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			String account = request.getParameter("account");
			String phone = request.getParameter("phone");
			String email = request.getParameter("email");
			User userInDb = userService.getUserByID(id);
			System.out.println(password);
			if ("".equals(password) || password == null) {
				password = userInDb.getPassword();
			}
			System.out.println(password);
			User user = new User(id, username, account, password, phone, email, null);
			if (userService.updateUser(user) > 0) {
				data.put("errorCode", 0);
				data.put("message", "修改成功！");
			}else {
				data.put("errorCode", -1);
				data.put("message", "修改失败！");
			}
		}
		String json = gson.toJson(data);
		response.getWriter().write(json);
		data.clear();
	}
}
